<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-input @keyup.enter.native="handleFilter" style="width: 200px;" class="filter-item" placeholder="搜索营业点名称或是联系号码" v-model="search.realname"></el-input>
      <el-button class="filter-item" type="primary" plain v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate" v-waves type="primary" v-roles="'editBillConfig'"  icon="el-icon-edit">添加站点分红比例</el-button>
    
   </div>
    <!-- // 这是列表 -->
    <el-table :data="listData" v-loading="listLoading" element-loading-text="给我一点时间" border fit highlight-current-row style="width: 100%">
      <el-table-column type="index"  align="center" label="序号" width="80"></el-table-column>
      <el-table-column align="center" label="站点名称"  width="150">
        <template slot-scope="scope">
          <span>{{scope.row.hotel_name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="联系号码"  width="110">
        <template slot-scope="scope">
          <span>{{scope.row.link_phone}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="结算方式"  width="120">
        <template slot-scope="scope">
          <el-tag type="danger">{{scope.row.cash_type | sourceFilter}}</el-tag>
        </template>
      </el-table-column>
        <el-table-column align="center" label="结算比例" width="100">
        <template slot-scope="scope">
          <span style="color:red;font-size:16px">{{scope.row.percent_num}} %</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="最后结算时间" width="100">
        <template slot-scope="scope">
          <span>{{scope.row.close_time}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="转账账号">
        <template slot-scope="scope">
          <span>{{scope.row.cash_account}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="转账户名">
        <template slot-scope="scope">
          <span>{{scope.row.cash_name}}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间"  width="100">
        <template slot-scope="scope">
          <span>{{scope.row.created_at}}</span>
        </template>
      </el-table-column>
      <!-- //这里是操作   -->
      <el-table-column align="center" label="操作"  width="125"  fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button  size="mini" type="primary" @click="handlEdit(scope.row)" v-roles="'editBillConfig'">编辑</el-button>
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="handleDel(scope.row)" v-roles="'delBillConfig'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--用户配置提成编辑窗口  -->
    <edit-bill-config :visible.sync="editVisible" ref="clickBill" :iscreated="iscreated" :propsData="propsData" @refresh='isrefresh'></edit-bill-config>  
    <!--编辑窗口  END -->

    <div class="pagination-container">
      <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page" :page-sizes="[10,20,30, 50]" :page-size="listQuery.pagesize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <!--可自定义按钮的样式、show/hide临界点、返回的位置  -->
    <!--如需文字提示，可在外部添加element的<el-tooltip></el-tooltip>元素  -->
    <el-tooltip placement="top" content="回到顶部">
      <back-to-top transitionName="fade" :customStyle="myBackToTopStyle" :visibilityHeight="300" :backPosition="50"></back-to-top>
    </el-tooltip>
  </div>
</template>


<script>

import waves from '@/directive/waves' // 水波纹指令
import BackToTop from '@/components/BackToTop' // 回到顶部
import * as api from '@/api/main-api' // API
import editBillConfig from '../orders/editUnitBillConfig'
import { mapGetters } from 'vuex'

export default {
  name: 'unitBillConfig',
  directives: {
    waves
  },
  components: { BackToTop, editBillConfig },

  computed: {
    ...mapGetters([
      'outlet_id'
    ])
  },

  data() {
    return {
      // 回到顶部样式
      myBackToTopStyle: {
        right: '50px',
        bottom: '50px',
        width: '40px',
        height: '40px',
        'border-radius': '4px',
        'line-height': '45px', // 请保持与高度一致以垂直居中 Please keep consistent with height to center vertically
        background: '#e7eaf1'// 按钮的背景颜色 The background color of the button
      },

      listQuery: {
        page: 1,
        pagesize: 10,
        unit_id: 0
      },

      // 搜索字段
      search: {
        realname: ''
      },
      total: 0,
      listLoading: false,
      listData: [],
      // 编辑传值
      editVisible: false,
      propsData: {},
      iscreated: 0 // 是编辑还是添加
    }
  },

  // 初始化数据
  created() {
    this.listQuery.unit_id = this.outlet_id
    this.fetchData()
  },

  filters: {
    sourceFilter(status) {
      if (status === '1') {
        return '每单百分比'
      } else {
        return '每单可得金额'
      }
    }
  },

  // 触动方法
  methods: {
    // 添加按钮
    handleCreate() {
      this.$refs.clickBill.getUnitList() // 执行子组件的方法
      this.propsData = {}
      this.iscreated = 1
      this.editVisible = !this.editVisible
    },

    // 编辑按钮
    handlEdit(row) {
      this.editVisible = !this.editVisible
      this.propsData = row
      this.iscreated = 0
    },

    // 子窗口关闭,刷新数据
    isrefresh(data) {
      this.fetchData()
    },

    // 控制每页调数
    handleSizeChange(val) {
      this.listQuery.pagesize = val
      this.fetchData()
    },

    // 跳页
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.fetchData()
    },

    // 搜搜
    handleFilter() {
      // 分页初始值
      this.listQuery.page = 1
      // 复制一个对象<br>var obj = { a: 1 ,b:2};
      this.listQuery = Object.assign(this.listQuery, this.search)
      this.fetchData()
    },

    // 获取数据
    fetchData() {
      this.listLoading = true
      api.unitBillConfigApi(this.listQuery).then(response => {
        if (response.data.code === 0) {
          this.listData = response.data.data
          this.total = parseInt(response.data.total)
        }
        this.listLoading = false
      })
    },

    // 删除数据操作
    handleDel(row) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        var params = {
          id: row.id
        }
        api.delbillConfigApi(params).then(response => {
          if (response.data.code === 0) {
            const index = this.listData.indexOf(row)
            this.listData.splice(index, 1)
            this.$root.message('已移除配置', 'success')
            this.total = this.total - 1
          }
        })
      }).catch(() => {
        this.$root.message('已取消删除', 'info')
      })
    }
  },

  // 监事事件
  watch: {
    outlet_id(id) {
      this.$nextTick(() => {
        setTimeout(() => {
          this.listQuery.unit_id = id
          this.fetchData()
        }, 20)
      })
    }
  }
}
</script>
